package com.zhuhao.export.exprotword;

import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Map;

/**
 * @Author zhuhao
 * @Date 2021/11/22 17:47
 * @desc
 */
public class DemoMain {

    public static void main(String[] args) {
        //模板文件地址
        String inputUrl = "E:\\template.docx";
        //新生成的word文件

        String outputUrl = String.valueOf(Thread.currentThread().getContextClassLoader().getResource("")) + "exportNew.docx";


        outputUrl = getResourceBasePath() + "exportNew.docx";

        System.out.println(outputUrl);
//
//        String outputUrl = "E:\\exportNew.docx";

        // 需要替换的内容  heheTest替换成666    word模板中需要这样写${heheTest}
        ProposalExportWordDTO proposalExportWordDTO = new ProposalExportWordDTO()
                .setAddress("合肥")
                .setUserName("小红")
                .setDuty("合肥市体育局秘书长")
                .setEmail("xxxx@gmail.com");

        Map<String, Object> proposalMap = BeanMapConvertUtil.beanToMap(proposalExportWordDTO);

        ExportWordDocxUtil.changWord(inputUrl, outputUrl, proposalMap);

        System.out.println("success");

//        System.out.println(getResourceBasePath());
    }

    /**
     * 获取项目根路径
     *
     * @return
     */
    private static String getResourceBasePath() {
        // 获取跟目录
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
        } catch (FileNotFoundException e) {
            // nothing to do
        }
        if (path == null || !path.exists()) {
            path = new File("");
        }

        String pathStr = path.getAbsolutePath();
        // 如果是在eclipse中运行，则和target同级目录,如果是jar部署到服务器，则默认和jar包同级
        pathStr = pathStr.replace("\\target\\classes", "\\");
        return pathStr;
    }

}
